#找到列表中的最大值和最小值

#使用内置函数max()和min()
numbers = [1,2,3,4,5]
max_value = max(numbers)
min_value = min(numbers)
print("最大值：",max_value)
print("最小值：",min_value)

'''
#使用循环遍历列表
numbers = [3,5,9,2,4,7]
#初始化最大值和最小值为列表的第一个元素
max_value = numbers[0]
min_value = numbers[0]
for num in numbers:
    if num > max_value:
        max_value = num
    if num < min_value:
        min_value = num
print("最大值：",max_value)
print("最小值：",min_value)

#使用排序(仅适用于需要同时对数据进行排序的场景)
numbers = [3,5,9,2,4,7]
#排序列表
sorted_numbers = sorted(numbers)
#最小值是第一个元素，最大值是最后一个元素
min_value = sorted_numbers[0]
max_value = sorted_numbers[-1]
print("最大值：",max_value)
print("最小值：",min_value)

#使用heapq模块(适合处理大数据集时的优化需求)
import heapq
numbers = [3,5,9,2,4,7]
max_value = heapq.nlargest(5,numbers)
min_value = heapq.nsmallest(5,numbers)
print("最大值：",max_value)
print("最小值：",min_value)

#使用生成器表达式
nested_list = [[3,1],[7,4],[9,2]]
max_value = max(max(sublist) for sublist in nested_list)
min_value = min(min(sublist) for sublist in nested_list)
print("最大值：",max_value)
print("最小值：",min_value)
'''